package com.yuxin.tongyi.lsll.select;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yuxin.tongyi.lsll.R;
import com.yuxin.tongyi.lsll.pojo.Event;
import com.yuxin.tongyi.lsll.pojo.EventInfo;
import com.yuxin.tongyi.lsll.pojo.NodeEvent;

/**
 * @author daijin'ge
 * 三级事件数据处理类
 */

public class ShowNodeEventDialog extends AlertDialog{

	private Event ne;
	private EventInfo ei;
	private List<NodeEvent> nodeEvent;
	private Context context;
	private CallBackInterface cbi;
	
	protected ShowNodeEventDialog(Context context,Event ne,EventInfo ei) {
		super(context);
		this.context = context;
		//在二级事件中初始化该类时，把一级二级事件作为参数传递过来
		this.ne = ne;
		this.ei = ei;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.node_event_list);
		//获得三级事件的数据
		nodeEvent = ne.getNodeEvent();
		ListView lv = (ListView) findViewById(R.id.select_event_list);
		NodeEventAdapter nea = new NodeEventAdapter(context);
		lv.setAdapter(nea);
		//设置三级事件节点的点击事件
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//在点击二级子节点时已经把CallBackInterface初始化，这里调用回调函数
				cbi.setReturnActivity(ei, ne, nodeEvent.get(position));
				ShowNodeEventDialog.this.cancel();
			}
		});
	}
	//定义接口
	public interface CallBackInterface{
		public void setReturnActivity(EventInfo ei,Event ne,NodeEvent nodeEvent);
	}
	//初始化接口方法
	public void setInterface(CallBackInterface cbi){
		this.cbi = cbi;
	}
	
	public final class ViewHolder {
		public TextView tvid;
		public TextView tvname;
	}
	class NodeEventAdapter extends BaseAdapter {
		
		private Context _context;
		private LayoutInflater inflater;

		NodeEventAdapter(Context context) {
			this._context = context;
			inflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			return nodeEvent.size();
		}

		@Override
		public Object getItem(int arg0) {
			return nodeEvent.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View view, ViewGroup arg2) {
			ViewHolder holder = null;
			if (view == null) {
				holder = new ViewHolder();
				view = inflater.inflate(R.layout.select_class_item, null);
				holder.tvid = (TextView) view.findViewById(R.id.select_class_tvitemid);
				holder.tvname = (TextView) view.findViewById(R.id.select_class_tvitem);
				//holder.tvname.setGravity(Gravity.CENTER);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}
			
			holder.tvid.setText(nodeEvent.get(position).getNodeEventId());
			holder.tvname.setText(nodeEvent.get(position).getNodeEventName());

			return view;
		}
		
	}
}
